package com.meiyou.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.meiyou.constant.MessageConstant;
import com.meiyou.context.BaseContext;
import com.meiyou.dto.AddCommentDto;
import com.meiyou.dto.CommentPageDto;
import com.meiyou.entity.Comment;
import com.meiyou.exception.ArticleNotFoundException;
import com.meiyou.exception.CommentNotFoundException;
import com.meiyou.mapper.ArticleMapper;
import com.meiyou.mapper.CommentMapper;
import com.meiyou.result.PageResult;
import com.meiyou.service.CommentService;
import com.meiyou.vo.ArticleDetailVo;
import com.meiyou.vo.ChildCommentVo;
import com.meiyou.vo.CommentVo;
import com.meiyou.vo.UserCommentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @BelongsProject: meiyou
 * @BelongsPackage: com.meiyou.service.impl
 * @Author: 尹晓安
 * @Date: 2023/12/3 20:55
 * @Description: todo
 */
@Service
public class CommentServiceImp implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 发布评论
     *
     * @param addCommentDto
     * @return
     */
    @Override
    public void addComment(AddCommentDto addCommentDto) {

        // 查询评论文章是否存在
        ArticleDetailVo article = articleMapper.selectById(addCommentDto.getArticleId());
        if (article == null) {
            throw new ArticleNotFoundException(MessageConstant.ARTICLE_NOT_FOUND);
        }

        Comment comment = new Comment();

        BeanUtils.copyProperties(addCommentDto, comment);

        comment.setUserId(BaseContext.getCurrentId());

        commentMapper.add(comment);

    }

    /**
     * 分页获取文章评论
     *
     * @param articleId
     * @param commentPageDto
     * @return
     */
    @Override
    public PageResult getCommentPage(Long articleId, CommentPageDto commentPageDto) {

        PageHelper.startPage(commentPageDto.getPage(), commentPageDto.getPageSize());

        Page<CommentVo> commentVo = commentMapper.getCommentPage(articleId);

        // 查询每条评论的子评论数
        commentVo.getResult().forEach(item -> {
            Integer count = commentMapper.selectCommentNum(item.getId(), null);
            item.setCommentNum(count);
            // 查询每条简略的字评论信息
            if (count > 0) {
                // 有子评论才去查询该评论的子评论
                item.setSimpleChildComment(commentMapper.selectSimpleChildComment(item.getId()));
            }
        });

        return new PageResult(commentVo.getTotal(), commentVo.getResult());
    }

    /**
     * 分页获取文章子评论
     *
     * @param commentId
     * @param commentPageDto
     * @return
     */
    @Override
    public PageResult getChildCommentPage(Long commentId, CommentPageDto commentPageDto) {

        PageHelper.startPage(commentPageDto.getPage(), commentPageDto.getPageSize());
        // 评论的点赞没有限制
        Page<ChildCommentVo> childCommentVo = commentMapper.getChildCommentPage(commentId);

        return new PageResult(childCommentVo.getTotal(), childCommentVo.getResult());
    }

    /**
     * 分页获取用户评论
     *
     * @param commentPageDto
     * @return
     */
    @Override
    public PageResult getUserCommentPage(CommentPageDto commentPageDto) {

        PageHelper.startPage(commentPageDto.getPage(), commentPageDto.getPageSize());

        Page<UserCommentVo> userCommentVo = commentMapper.getUserCommentPage(BaseContext.getCurrentId());

        return new PageResult(userCommentVo.getTotal(), userCommentVo.getResult());
    }

    /**
     * 删除评论
     *
     * @param id 评论id
     * @return
     */
    @Override
    public void deleteComment(Long id) {

        // 查询该评论是否存在
        Integer count = commentMapper.select(id);

        if (count <= 0) {
            throw new CommentNotFoundException(MessageConstant.COMMENT_NOT_FOUND);
        }

        commentMapper.remove(id);

    }

    /**
     * 点赞评论
     *
     * @param id 评论id
     * @return
     */
    @Override
    public void starComment(Long id) {

        // 查询该评论点赞数
        Integer starNum = commentMapper.selectStarNum(id);

        commentMapper.starComment(id, starNum + 1);

    }
}
